﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Threading;
using System.Media;

namespace OrganaiserProgram
{
    public static class DB
    {
        public static List<EqualaiserSet> InfoList = new List<EqualaiserSet>();

        private const string FILE_NAME = "data.bin";

        public static void Load()
        {
            if (File.Exists(FILE_NAME) == false) return;

            using (Stream stream = File.Open(FILE_NAME, FileMode.Open))
            {
                BinaryFormatter bin = new BinaryFormatter();
                try
                {
                    InfoList = (List<EqualaiserSet>)bin.Deserialize(stream);
                }
                catch (Exception e)
                {

                }
            }
        }

        public static void Save()
        {
            using (Stream stream = File.Open(FILE_NAME, FileMode.OpenOrCreate))
            {
                BinaryFormatter bin = new BinaryFormatter();
                bin.Serialize(stream, InfoList);
            }
        }
    }

    static class Program
    {
        static void playSimpleSound(string text, string fileName)
        {
            bool var = false;

            if (fileName != null)
            {
                SoundPlayer player = new SoundPlayer(@fileName);
                player.Play();

                if (text == null)
                {
                    text = "WAKE UUUUUUPPPPPPP!!!!!";
                    var = true;
                }
                MessageBox.Show(text);
                if (var)
                {
                    text = null;
                    var = false;
                }
                player.Stop();
            }
            else
            {
                if (text == null)
                {
                    text = "WAKE UUUUUUPPPPPPP!!!!!";
                    var = true;
                }
                MessageBox.Show(text);
                if (var)
                {
                    text = null;
                    var = false;
                }
            }
        }
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            DB.Load();

            Thread myThread = new System.Threading.Thread(new System.Threading.ThreadStart(CheckEvents));
            myThread.Start();

            Application.Run(new Form1());
            myThread.Abort();
            DB.Save();
        }

        static void CheckEvents()
        {
            while(true)
            {
                foreach (var timeEvent in DB.InfoList)
                {
                    var date = Convert.ToDateTime(timeEvent.TargetDate);
                    var time = Convert.ToDateTime(timeEvent.EventTime);
                    if (date.Year == DateTime.Now.Year && date.Month == DateTime.Now.Month && date.Day == DateTime.Now.Day && time.Hour == DateTime.Now.Hour && time.Minute == DateTime.Now.Minute)
                    {
                        playSimpleSound(timeEvent.EventText, timeEvent.EventSound);
                    }
                }
                Thread.Sleep(15000);
            }
        }
    }
}
